package com.qingke.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qingke.exceptions.NotFoundException;
import com.qingke.exceptions.WrongParameterException;

/**
 * Servlet implementation class ServletApp
 */
@WebServlet("/ServletApp")
public class ServletApp extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connection conn = null;
	private PreparedStatement pst = null;
	private ResultSet rs = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletApp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/servletbase?useSSL=false";
		String user = "root";
		String password = "lz*19950926";
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Enumeration<String> enums = request.getParameterNames();
//		Integer id = null;
//		String name = null;
//		while(enums.hasMoreElements()){
//			String ele = enums.nextElement();
//			if("id".equals(ele)){
//				id=Integer.parseInt(request.getParameter(ele));					
//			}else if("name".equals(ele)){
//				name = request.getParameter(ele);
//			}else {
//				throw new WrongParameterException();
//			}
//		}
//		
//		if(id==null||name==null){
//			throw new WrongParameterException();
//		}
//		
//		String sql = "select * from user where id = ?";
//		try {
//			pst = conn.prepareStatement(sql);
//			pst.setInt(1, id);
//			rs = pst.executeQuery();
//			if (rs==null) {
//				throw new NotFoundException();
//			}
//			
//			while(rs.next()){
//				String realName = rs.getString("name");
//				if(!name.equals(realName)){
//					throw new NotFoundException();
//				}else {
//					response.sendError(500);
//				}
//			}
////			response.setContentType("text/plain;charset=utf-8");
////			response.getWriter().append("登录成功");
//		} catch (SQLException e) {
//			
//		}
//		
//		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath()+"\n");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
